
var CardDimensions = function(config) {
	if ( config ) $.extend( this, config );
	
	if (typeof this.width == 'undefined') this.width = '5in';
	if (typeof this.height == 'undefined') this.height = '3in';

	this.width += '';
	this.height += '';
	
	this.width = this.width.replace('in','') * 1;
	this.height = this.height.replace('in','') * 1;
	
	// fontSize : Number of characters per inch
	this.fontSize = {14 : 12, 12 : 13.75, 10 : 16};
	this.defaultFontSize = 14;
	this.minFontSize = 10;
	this.maxFontSize = 14;
	
	var me = this;
	
	this.getLowerFontSize = function(font) {
		var fontObj = {14:12, 12:10};
		return (fontObj.hasOwnProperty(font)) ? fontObj.font : false;
	}
	
	this.getCharCount = function(percent,font) {
		if (typeof percent == 'undefined' || !percent) return false;
		if (typeof font == 'undefined' || !font || !me.fontSize.hasOwnProperty(font)) font = me.defaultFontSize;
		
		var width = me.width * (percent/100);
		var chars = width * me.fontSize[font];
		return Math.floor(chars);
	}
	
	this.getWidth = function(percent) {
		if (typeof percent == 'undefined' || !percent) return false;
		return ((me.width * (percent/100)) + 'in');
	}
/*
	this.getFont = function (charCount, value) {
		var tc, ct, font = false;
		tc = value.length;
		_.each(me.fontSize, function(vl,key,lst){
			if(charCount > tc) {
				font = key;
				// break;
				return font;
			}
			
		});
		return font;
	}
*/

	this.getFont = function (percent, value) {
		var tc, ct, font = false;
		tc = value.length;
		_.each(me.fontSize, function(vl,key,lst){
			ct = me.getCharCount(percent, key);
			// console.log(ct + ' ' + tc + ' ' + key );
			if(ct > tc) font = key;
		});
		return font;
	}
	

	this.getLines = function (charCount, value) {
		var tst = value;
		var flag = true;
		var ar = [];
		var pos;
		var tmp;
		while(flag) {
			if(tst.length > (charCount-1)) {
				if(tst[charCount] == ' ') {
					tmp = tst.substr(0,charCount-1);
					ar.push(tmp);
					tst = tst.substr(charCount);
				} else {
					tmp = tst.substr(0,charCount-1);
					pos = strrpos(tmp,' ');
					if(false === pos) {
						ar.push(tmp);
						tst = tst.substr(charCount);
					} else {
						ar.push(tst.substr(0,pos + 1));
						tst = tst.substr(pos + 1);
					}
				}
			} else {
				ar.push(tst);
				flag = false;
			}
		}
		return ar;
	}

}

var CardSide = function( config ) {

	if ( config ) $.extend( this, config );
	
	this.output = 'Test';
	
	if (typeof this.data == 'undefined' || !this.data || typeof this.template == 'undefined' || !this.template ) {
		console.log( 'valid data and template must be specified to instantiate this program.' ) 
	}
	if (typeof this.width == 'undefined') this.width = '5in';
	if (typeof this.height == 'undefined') this.height = '3in';
	this.cardDimensions = new CardDimensions({width : this.width, height : this.height});
	
	var me = this;
		
	this.getCard = function() {
		// return 'In function';
		return me.output;
	}
	
	this.createCard = function () {
		var dataHtml = '',i,j,entity,tmp,tc,dc,lp,width,ar,tempc,font;
		var padStr = ' ';
		var fontclass, extraclass;
		for(i=0; i< me.template.length; i++) {
			// dataHtml += '<br style="clear: left;" >'+'<div style=\"width:'+me.width+'in;\">';
			dataHtml += '<span style="clear: left;" ></span>'+'<div class="left" style=\"width:'+me.width+'in;\">';
			for(j=0; j< me.template[i].length; j++) {
				entity = me.template[i][j];

				tc = me.cardDimensions.getCharCount(entity.percent);
				dc = entity.key.text.length + (me.data.hasOwnProperty(entity.value.text) ? me.data[entity.value.text].length : 0);

				if(me.data.hasOwnProperty(entity.value.text)) {
					tmp = entity.key.text + me.data[entity.value.text];
				} else {
					tmp = entity.key.text;
				}
				
				fontclass = 'class' + me.cardDimensions.defaultFontSize;
				extraclass = ('undefined' == typeof entity.key.class) ? '' : entity.key.class;
				
				if(tc >= dc) {
					
					// lp = Math.floor(tc/2) - Math.floor(dc/2);
					switch(entity.align) {
						case 'left':
							// tmp = str_pad(tmp,tc, padStr,'STR_PAD_RIGHT');
							tempc = entity.key.text.length;
							tmp = '<span class="' + extraclass + '" >'+ entity.key.text + '</span>' 
							+ '<span class="' + extraclass + '" >' + str_pad(me.data[entity.value.text],tc - tempc, padStr,'STR_PAD_RIGHT') + '</span>';
							break;
						case 'right':
							// tmp = str_pad(tmp,tc, padStr,'STR_PAD_LEFT');
							tempc = me.data[entity.value.text].length;
							tmp = '<span class="' + extraclass + '" >'+ str_pad(entity.key.text,tc - tempc, padStr,'STR_PAD_LEFT') + '</span>' 
							+ '<span class="' + extraclass + '" >' + me.data[entity.value.text] + '</span>';
							break;
						case 'center':
						default:
							tmp = str_pad(tmp,tc, padStr,'STR_PAD_BOTH');
							break;
					}
					// console.log(tmp);
					width = me.cardDimensions.getWidth(entity.percent);
					// dataHtml += '<div class="left '+entity.value.class+'" style="width:' + width + 'in;" class="font14">' + tmp + '</div>';
					dataHtml += '<div class="left '+fontclass+'" style="width:' + width + ';" >' + tmp + '</div>';
				} else {
					/*
					ar = str_split(tmp, tc);
					for(var k=0;k<ar.length;k++) {
						dataHtml += '<div class="left '+entity.value.class+'" style="width:' + me.width + 'in;" class="font14">' + ar[k] + '</div>';
					}
					*/
					if('undefined' != typeof(entity.lines) && entity.lines > 1) {
						ar = me.cardDimensions.getLines(tc,tmp);
						for(var k=0;k<ar.length;k++) {
							if(k == 0) {
								dataHtml += '<div class="left '+fontclass+''+ extraclass +'" style="width:' + width + ';" ><span class="' + extraclass + '" >' + trim(ar[k]) + '</span></div>';
							} else {
								dataHtml += '</div><span style="clear: left;" ></span>'+'<div class="left" style=\"width:'+me.width+'in;\">';
								dataHtml += '<div class="left '+extraclass+' '+fontclass+'" style="width:' + width + ';"><span class="' + extraclass + '" >' + trim(ar[k]) + '</span></div>';
							}
						}
					} else {
						font = me.cardDimensions.getFont(entity.percent,tmp);
						fontclass = 'class' + font;
						// console.log(fontclass);
						dataHtml += '<div class="left '+fontclass+'" style="width:' + width + ';" ><span class="' + entity.value.class + '" >' + tmp + '</span></div>';
					}
					
				}
			}
			dataHtml += '</div>';
			// dataHtml += '</span>';
		}
		me.output = me.getDiv(dataHtml);
	}
	
	this.getDiv = function (dataHtml) {
		var card = '<div style="width:'+ me.width +'in; height:'+ me.height +'in; border: thin solid #000; padding: 15px;">';
		// var card = '<div style="width:6in; height:'+ me.height +'; border: thin solid #000; padding: 15px;">';
		card += dataHtml;
		card += '</div>';
		return card;
	}

	
}

var Card = function( config ) {
	if ( config ) $.extend( this, config );
	
	if (typeof this.width == 'undefined') this.width = '5in';
	if (typeof this.height == 'undefined') this.height = '3in';

	this.pageHeight = 11;
	this.pageWidth = 8.5;
	this.contentHeight = 10; // excludes an upper and lower .5 inch
	this.duplexFlag = false;
	
	if (typeof this.data == 'undefined' || !this.data) {
		console.log( 'valid data must be specified to instantiate this program.' );
	}
	
	if((typeof this.templateFront != 'undefined' && this.templateFront) && (typeof this.templateBack != 'undefined' && this.templateBack)) {
		this.duplexFlag = true;
	}
	
	if (typeof this.templateFront == 'undefined' || !this.templateFront) {
		console.log( 'valid templateFront must be specified to instantiate this program.' ) ;
		return;
	}
	
	if (typeof this.width == 'undefined') this.width = '5in';
	if (typeof this.height == 'undefined') this.height = '3in';

	this.width += '';
	this.height += '';
	this.width = this.width.replace('in','') * 1;
	this.height = this.height.replace('in','') * 1;
	
	var me = this;
	
	this.getCardCount = function() {
		var no = Math.floor(me.contentHeight/me.height);
		var n = no-1;
		// 1/6 inch to compensate for <br>, .014 for the top/bottom borders of individual cards.
		if(((no * me.height) + (n * (1/6)) + (no * .014)) > 10) {
			no = n;
		}
		return no;
	}

	this.renderPage = function() {
		var page = '', front = [], back = [], data = '', card;
		var count = 0;
		var pageHeadFront = 	'<div style="width:8.5in; height:11in; border: thin solid red; padding: 0px;">'
						+'<div class="font14" style="border: thin solid #000;height:.5in;width:8.5in;" >Top Space</div>';
		var pageHeadBack = 	'<div style="width:8.5in; height:11in; border: thin solid red; padding: 0px;" align="right">'
						+'<div class="font14" style="border: thin solid #000;height:.5in;width:8.5in;" >Top Space</div>';
		var pageTail = '</div>';
		var pageBreak = '<div class="break">Page Break</div>';
		
		var cardCount = me.getCardCount();
		
		
		for(var i=0; i<me.data.length; i++) {
			count++;
			data = me.data[i];
			card = new CardSide({data:data, template: me.templateFront, width: me.width, height: me.height});
			card.createCard();
			front.push(card.getCard());
			if(me.duplexFlag) {
				card = new CardSide({data:data, template: me.templateBack, width: me.width, height: me.height});
				card.createCard();
				back.push(card.getCard());
			}
			if(count >= cardCount) {
				page += pageHeadFront + front.join('<br>') + pageTail + pageBreak;
				if(me.duplexFlag) {
					page += pageHeadBack + back.join('<br>') + pageTail + pageBreak;
				}
				count = 0;
				front = [];back = [];
			}
		}
		if(front.length) {
			page += pageHeadFront + front.join('<br>') + pageTail + pageBreak;
			if(me.duplexFlag) {
				page += pageHeadBack + back.join('<br>') + pageTail + pageBreak;
			}

		}
		return page;
	}
	
}



